package com.dormms.service.impl;

import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.dormms.bean.DormAdminInfo;
import com.dormms.bean.DormLocation;
import com.dormms.dao.DormDao;
import com.dormms.dao.DormLocationDao;
import com.dormms.dto.DormLocationAdmin;
import com.dormms.dto.DormLocationAdminInfo;
import com.dormms.service.DormLocationService;
@Service
public class DormLocationServiceImpl implements DormLocationService{
	@Autowired
	private DormLocationDao dormLocationDao;
	@Autowired
	private DormDao dormDao;

	public List<DormLocation> queryDormLocationInfoByDormLocation(DormLocation dormLocation) {
		// TODO Auto-generated method stub
		return dormLocationDao.queryDormLocationInfo(dormLocation);
	}

	public Integer insertDormLocationInfo(DormLocation dormLocation) {
		// TODO Auto-generated method stub
		//判断宿舍楼是否已存在
		Integer dormLocationId = dormLocationDao.isDormLocationExist(dormLocation.getDormLocationId());
		if(dormLocationId != null) {
			return 0;
		}
		dormLocationDao.insertDormLocationInfo(dormLocation);
		return dormLocation.getDormLocationId();
	}
	
	public Integer insertDormLocationAdminInfo(DormLocationAdmin dormLocationAdmin) {
		//判断宿舍楼是否已存在 ,宿舍管理员是否被分配
		Integer dormLocationId = dormLocationDao.isDormLocationExist(dormLocationAdmin.getDormLocationId());
		String dormAdminId = dormLocationDao.isDormLocationAdminExist(dormLocationAdmin.getDormAdminId());
		if(dormLocationId != null && !dormLocationAdmin.getDormAdminId().equals(dormAdminId)) {
			dormLocationAdmin.setAdminStart(new Date());
			dormLocationAdmin.setAdminStatus(1);
			dormLocationDao.insertDromLocationAdmin(dormLocationAdmin);
			return dormLocationAdmin.getDormAdminDormlocationId();
		}
		return 0;
	}
	
	public List<DormAdminInfo> getNoAdminDormLocationDormAdmin(){
		return dormLocationDao.queryNoAdminDormLocationDormAdmin();
	}

	public DormLocationAdminInfo getAdminDormLocationAllDormAdmin(Integer dormLocationId) {
		// TODO Auto-generated method stub
		DormLocationAdminInfo dormLocationAdminInfo = new DormLocationAdminInfo();
		dormLocationAdminInfo.setDormLocation(dormLocationDao.queryDormLocationByDormLocationId(dormLocationId));
		dormLocationAdminInfo.setAdminInfos(dormLocationDao.queryAdminDormLocationAllDormAdminByDormLocationId(dormLocationId));
		return dormLocationAdminInfo;
	}

	public Integer updateAdminDormLocationInfoBySuper(DormLocation dormLocation) {
		// TODO Auto-generated method stub
		return dormLocationDao.updateAdminDormLocationInfoBySuper(dormLocation);
	}

	public Integer deleteDormLocationAdmin(String dormAdminId) {
		// TODO Auto-generated method stub
		return dormLocationDao.deleteDromLocationAdminByDormAdminId(dormAdminId);
	}

}
